import http from '../api/http'
import type { ApiResult, PageRequest, PageResult } from './types'

// 日志查询请求参数
export interface LogQueryRequest extends PageRequest {
  startTime: string // 开始时间
  endTime: string   // 结束时间
  key?: string      // 搜索关键字
}

// 行为日志
export interface ActionLog {
  id?: string
  userId?: string
  type?: string
  action?: string
  version?: string
  apiUrl?: string
  ip?: string
  params?: string
  result?: number
  resultMessage?: string
  creationTime?: string
}

// 错误日志
export interface ErrorLog {
  id?: string
  userId?: string
  message?: string
  api?: string
  ip?: string
  params?: string
  creationTime?: string
}

// 登录日志
export interface LoginLog {
  id?: string
  account?: string
  userId?: string
  result?: number
  ip?: string
  code?: string
  type?: string
  explain?: string
  creationTime?: string
}

// 日志服务类
export class LogService {
  /**
   * 获取行为日志列表
   */
  static async getActionLogs(params: LogQueryRequest): Promise<ApiResult<PageResult<ActionLog>>> {
    const resp = await http.get<ApiResult<PageResult<ActionLog>>>('/Log/GetActionLogs', { params })
    return resp.data
  }

  /**
   * 获取错误日志列表
   */
  static async getErrorLogs(params: LogQueryRequest): Promise<ApiResult<PageResult<ErrorLog>>> {
    const resp = await http.get<ApiResult<PageResult<ErrorLog>>>('/Log/GetErrorLogs', { params })
    return resp.data
  }

  /**
   * 获取登录日志列表
   */
  static async getLoginLogs(params: LogQueryRequest): Promise<ApiResult<PageResult<LoginLog>>> {
    const resp = await http.get<ApiResult<PageResult<LoginLog>>>('/Log/GetLoginLogs', { params })
    return resp.data
  }
}

// 日志类型枚举
export const LogType = {
  ACTION: 'action',
  ERROR: 'error',
  LOGIN: 'login'
} as const

// 操作结果枚举
export const OperationResult = {
  SUCCESS: 1,
  FAILURE: 0
} as const

// 登录结果枚举
export const LoginResult = {
  SUCCESS: 1,
  FAILURE: 0
} as const

// 格式化日志时间
export const formatLogTime = (time?: string): string => {
  if (!time) return '-'
  return new Date(time).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 获取操作结果文本
export const getResultText = (result?: number): string => {
  switch (result) {
    case OperationResult.SUCCESS:
      return '成功'
    case OperationResult.FAILURE:
      return '失败'
    default:
      return '未知'
  }
}

// 获取操作结果颜色
export const getResultColor = (result?: number): string => {
  switch (result) {
    case OperationResult.SUCCESS:
      return 'success'
    case OperationResult.FAILURE:
      return 'danger'
    default:
      return 'secondary'
  }
}

// 获取登录结果文本
export const getLoginResultText = (result?: number): string => {
  switch (result) {
    case LoginResult.SUCCESS:
      return '登录成功'
    case LoginResult.FAILURE:
      return '登录失败'
    default:
      return '未知'
  }
}

// 获取登录结果颜色
export const getLoginResultColor = (result?: number): string => {
  switch (result) {
    case LoginResult.SUCCESS:
      return 'success'
    case LoginResult.FAILURE:
      return 'danger'
    default:
      return 'secondary'
  }
}
